package com.yicong.ants.ui.present;

import android.content.Context;
import android.content.res.Configuration;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;

import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.yicong.ants.R;
import com.yicong.ants.bean.ExchangeNoticeBean;
import com.yicong.ants.bean.task.TaskInfo;
import com.yicong.ants.databinding.ActivityExchangeYcBinding;
import com.yicong.ants.databinding.ItemExchangeBinding;
import com.yicong.ants.manager.AppHelper;
import com.yicong.ants.manager.UserManager;
import com.yicong.ants.manager.business.Dialogs;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.utils.AppUtils2;
import com.yicong.ants.utils.MyRunnable;
import com.yicong.ants.utils.ToastUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 门票兑换Yc
 */
public class ExchangeYcActivity extends BaseTitleBarActivity<ActivityExchangeYcBinding> implements View.OnClickListener {
    @Override
    protected int getLayout() {
        return R.layout.activity_exchange_yc;
    }

    private List<TaskInfo.ListBean> list;
    private ExchangeNoticeBean exchangeNoticeBean;
    private boolean isFirst = true;

    @Override
    protected void initEventAndData() {
        setTitleBarVisible(false);
        AppHelper.transStatus(getWindow());
        mDataBind.setClick(this);

//        boolean b = (boolean) SPUtils.get(this, "YCExchangeNotice", false);
//        if (!b) {
//            getYcExchangeNotice();
//        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        onLoadData();
    }

    private void getYcExchangeNotice() {
        if (exchangeNoticeBean == null) {
            showProgress();
            addSubscribe(RetrofitHelper.getApis().getCoinExchangeInfo().compose(RxHelper.toMain()).subscribe(respBean -> {
                hideProgress();
                if ("2000".equals(respBean.getCode())) {
                    exchangeNoticeBean = respBean.getData();
                    Dialogs.showYcExchangeNotice(thisActivity, exchangeNoticeBean, new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    });
                    isFirst = false;
                    onLoadData();
                } else if ("3000".equals(respBean.getCode())) {
                    UserManager.routeLogin(mContext);
                    finish();
                }
            }));
        }
//        else {
//            Dialogs.showYcExchangeNotice(thisActivity, exchangeNoticeBean, null);
//        }
    }

    private void updatePage(List<TaskInfo.ListBean> list) {
        this.list = list;
        mDataBind.content.removeAllViews();
        if (list == null) return;
        for (int i = 0; i < list.size(); i++) {
            ItemExchangeBinding binding = DataBindingUtil.inflate(mLayoutInflater, R.layout.item_exchange, null, false);
            binding.setBean(list.get(i));
            int finalI = i;
            binding.setClick(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //确认密码对话框
                    exchange(list.get(finalI).getExchange_coin(), list.get(finalI).getTask_id());
                }
            });
            if (list.get(i).getStatus() != 1) {
                binding.exchangeTask.setText("不可兑换");
                binding.exchangeTask.setTextColor(ContextCompat.getColor(this, R.color.new_text_color_3));
                binding.exchangeTask.setBackgroundResource(R.drawable.new_btn_bg_grey);
                binding.exchangeTask.setEnabled(false);
                binding.exchangeTask.setClickable(false);
            } else {
                if (list.get(i).getAllow_exchange_num() <= 0) {
                    binding.exchangeTask.setText("次数不足");
                    binding.exchangeTask.setTextColor(ContextCompat.getColor(this, R.color.white));
                    binding.exchangeTask.setBackgroundResource(R.drawable.new_btn_bg_disable_round);
                    binding.exchangeTask.setEnabled(false);
                    binding.exchangeTask.setClickable(false);
                }
            }
            mDataBind.content.addView(binding.getRoot());
        }
        getYcExchangeNotice();
    }

    private void exchange(String exchangeCoin, int taskId) {
//        boolean b = (boolean) SPUtils.get(thisActivity, "YCExchangeNotice", false);
//        if (!b) {
//            getYcExchangeNotice();
//        } else {
        Dialogs.showRedemptionConfirmation(mContext, exchangeCoin, new MyRunnable<String>() {
            @Override
            public void run(String s) {
                Map<String, String> map = new HashMap<>();
                map.put("task_id", String.valueOf(taskId));
                map.put("pwd", s);
                addSubscribe(RetrofitHelper.getApis().coinFetchTask(map).compose(RxHelper.toMain()).subscribe(respBean -> {
                    if ("2000".equals(respBean.getCode())) {
                        //领取成功对话框
                        Dialogs.showGetTaskSuc(mContext, ExchangeYcActivity.this::onLoadData);
                    } else if ("2005".equals(respBean.getCode())) {
                        //任务领取失败
                        Dialogs.showGetTaskFail(mContext, null);
                    } else {
                        ToastUtils.showShort(thisActivity, "" + respBean.getMsg());
                    }
                }));
            }
        });
//        }
    }

    @Override
    protected void onLoadData() {
        addSubscribe(RetrofitHelper.getApis().coinExchangeTaskInfo().compose(RxHelper.toMain()).subscribe(respBean -> {
            if ("2000".equals(respBean.getCode())) {
                updatePage(respBean.getData());
            }
        }));
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.back) {
            finish();
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        Configuration config = base.getResources().getConfiguration();
        config.fontScale = 1f;
        config.densityDpi = (int) ((UiHelper.getScreenWidth() / 411 + 0.5) * 160);
        Context context = base.createConfigurationContext(config);
        super.attachBaseContext(Objects.requireNonNull(new AppUtils2().attachBaseContext(context)));
    }
}
